package com.lsyw.intergrate.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lsyw.intergrate.common.BaseResult;
import com.lsyw.intergrate.entity.TransportWorkOrderAlarm;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsyw.intergrate.common.Result;
import org.springframework.util.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import com.lsyw.intergrate.service.TransportWorkOrderAbnormalService;
import com.lsyw.intergrate.entity.TransportWorkOrderAbnormal;

import java.util.*;

import io.swagger.annotations.Api;
import lombok.Data;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 运输工单告警信息 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-12-26
 */
@Api(tags = "运输工单异常信息")
@RestController
@RequestMapping("/transport-work-order-abnormal")
public class TransportWorkOrderAbnormalController {


    @Autowired
    private TransportWorkOrderAbnormalService transportWorkOrderAbnormalService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(transportWorkOrderAbnormalService.getInfoById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody TransportWorkOrderAbnormal transportWorkOrderAbnormal) {
        String loginId;
        try {
            loginId = StpUtil.getLoginIdAsString();
        } catch (Exception e) {
            return new Result(4414, "登录过期，请重新登录", "登录过期，请重新登录");
        }
        transportWorkOrderAbnormal.setGmtCreate(new Date());
        transportWorkOrderAbnormal.setDelFlag(0);
        transportWorkOrderAbnormal.setStatus(1);
        transportWorkOrderAbnormal.setGmtCreateUser(loginId);
        transportWorkOrderAbnormal.setAbnormalDate(new Date());
        transportWorkOrderAbnormalService.save(transportWorkOrderAbnormal);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            transportWorkOrderAbnormalService.removeById(Long.parseLong(id));
        }
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody TransportWorkOrderAbnormal transportWorkOrderAbnormal) {
        String loginId;
        try {
            loginId = StpUtil.getLoginIdAsString();
        } catch (Exception e) {
            return new Result(4414, "登录过期，请重新登录", "登录过期，请重新登录");
        }
        transportWorkOrderAbnormal.setGmtModified(new Date());
        transportWorkOrderAbnormal.setGmtModifiedUser(loginId);
        transportWorkOrderAbnormalService.updateById(transportWorkOrderAbnormal);
        return Result.successResult(null);
    }

    /**
     * 通过id查询
     */
    @GetMapping("/updateStatus")
    public Result updateStatus(String id,Integer status) {
        TransportWorkOrderAbnormal transportWorkOrderAbnormal = transportWorkOrderAbnormalService.getById(id);
        transportWorkOrderAbnormal.setStatus(status);
        transportWorkOrderAbnormalService.updateById(transportWorkOrderAbnormal);
        return Result.successResult("");
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "运输历史记录列表-分页", notes = "运输历史记录列表-分页", httpMethod = "POST")
    public BaseResult<IPage<TransportWorkOrderAbnormal>> page(@RequestBody PageInfo pageInfo) {
        Page<TransportWorkOrderAbnormal> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        return BaseResult.successResult(this.transportWorkOrderAbnormalService.pageList(page, pageInfo.getCondition()));
    }

}
